<?php
require_once(SYSBASE.'Libraries/l10n/streams.php');
require_once(SYSBASE.'Libraries/l10n/gettext.php');

// Variables
global $text_domains,$current_locale,$default_domain;
$text_domains = array();
$current_locale = null;
$default_domain = 'message';

function set_locale($locale = 'zh_CN') {
    global $current_locale;

    $current_locale = $locale;
}

function get_locale() {
    global $current_locale,$LANG;

    if(isset($current_locale))
        return $current_locale;

    $current_locale = isset($LANG['default_language'])?$LANG['default_language']:'zh_CN';
    return $current_locale;
}
/**
 * Utility function to get a StreamReader for the given text domain.
 */
function _get_reader($domain=null,$category = 5, $enable_cache=true) {
    global $text_domains,$default_domain;

    if (!isset($domain)) $domain = $default_domain;

    if (!isset($text_domains[$domain]->l10n)) {

        $p = isset($text_domains[$domain]->path) ? $text_domains[$domain]->path : './';

        $locale = get_locale();
        
        $categorylist = array('LC_CTYPE', 'LC_NUMERIC', 'LC_TIME', 'LC_COLLATE', 'LC_MONETARY', 'LC_MESSAGES', 'LC_ALL');

        $path = $p . "$locale/".$categorylist[$category]."/$domain.mo";

        if (file_exists($path)) {
            $input = new FileReader($path);
        }
        else {
            $input = null;
        }
        $text_domains[$domain]->l10n = new gettext_reader($input, $enable_cache);
    }
    return $text_domains[$domain]->l10n;
}

/**
 * Get the codeset for the given domain.
 */
function _get_codeset($domain=null) {
    global $text_domains, $default_domain, $LC_CATEGORIES;
    if (!isset($domain)) $domain = $default_domain;
    return (isset($text_domains[$domain]->codeset))? $text_domains[$domain]->codeset : ini_get('mbstring.internal_encoding');
}

/**
 * Convert the given string to the encoding set by bind_textdomain_codeset.
 */
function _encode($text,$domain = null) {
    $source_encoding = @mb_detect_encoding($text);
    $target_encoding = _get_codeset($domain);
    if ($source_encoding != $target_encoding) {
        if(function_exists("mb_convert_encoding")){
            return mb_convert_encoding($text, $target_encoding, $source_encoding);
        }else{
            return $text;
        }
    }else {
        return $text;
    }
}

/**
 * Sets the path for a domain.
 */
function _bindtextdomain($domain, $path) {
    global $text_domains;
    // ensure $path ends with a slash
    if ($path[strlen($path) - 1] != '/') $path .= '/';
    elseif ($path[strlen($path) - 1] != '\\') $path .= '\\';
    $text_domains[$domain]->path = $path;
}

/**
 * Specify the character encoding in which the messages from the DOMAIN message catalog will be returned.
 */
function _bind_textdomain_codeset($domain, $codeset) {
    global $text_domains;
    $text_domains[$domain]->codeset = $codeset;
}

/**
 * Sets the default domain.
 */
function _textdomain($domain) {
    global $default_domain;
    $default_domain = $domain;
}

/**
 * Lookup a message in the current domain.
 */
function _gettext($msgid) {
    $l10n = _get_reader();
    //return $l10n->translate($msgid);
    return _encode($l10n->translate($msgid));
}
/**
 * Alias for gettext.
 */
function __($msgid) {
    return _gettext($msgid);
}

function __T($msgid) {
    return _dgettext('system',$msgid);
}
/**
 * Plural version of gettext.
 */
function _ngettext($single, $plural, $number) {
    $l10n = _get_reader();
    //return $l10n->ngettext($single, $plural, $number);
    return _encode($l10n->ngettext($single, $plural, $number));
}

/**
 * Override the current domain.
 */
function _dgettext($domain, $msgid) {
    $l10n = _get_reader($domain);
    //return $l10n->translate($msgid);
    return _encode($l10n->translate($msgid),$domain);
}
/**
 * Plural version of dgettext.
 */
function _dngettext($domain, $single, $plural, $number) {
    $l10n = _get_reader($domain);
    //return $l10n->ngettext($single, $plural, $number);
    return _encode($l10n->ngettext($single, $plural, $number),$domain);
}

/**
 * Overrides the domain and category for a single lookup.
 */
function _dcgettext($domain, $msgid, $category) {
    $l10n = _get_reader($domain, $category);
    //return $l10n->translate($msgid);
    return _encode($l10n->translate($msgid),$domain);
}
/**
 * Plural version of dcgettext.
 */
function _dcngettext($domain, $single, $plural, $number, $category) {
    $l10n = _get_reader($domain, $category);
    //return $l10n->ngettext($single, $plural, $number);
    return _encode($l10n->ngettext($single, $plural, $number),$domain);
}


// Wrappers used as a drop in replacement for the standard gettext functions

if (!function_exists('gettext')) {
    function bindtextdomain($domain, $path) {
        return _bindtextdomain($domain, $path);
    }
    function bind_textdomain_codeset($domain, $codeset) {
        return _bind_textdomain_codeset($domain, $codeset);
    }
    function textdomain($domain) {
        return _textdomain($domain);
    }
    function gettext($msgid) {
        return _gettext($msgid);
    }
    function _($msgid) {
        return __($msgid);
    }
    function ngettext($single, $plural, $number) {
        return _ngettext($single, $plural, $number);
    }
    function dgettext($domain, $msgid) {
        return _dgettext($domain, $msgid);
    }
    function dngettext($domain, $single, $plural, $number) {
        return _dngettext($domain, $single, $plural, $number);
    }
    function dcgettext($domain, $msgid, $category) {
        return _dcgettext($domain, $msgid, $category);
    }
    function dcngettext($domain, $single, $plural, $number, $category) {
        return _dcngettext($domain, $single, $plural, $number, $category);
    }
}